package com.fudan.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fudan.constants.ReponseStatus;
import com.fudan.dto.entities.User;
import com.fudan.service.UserServiceImpl;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:4200")

public class UserController {
	
	@Autowired
	private UserServiceImpl userService;
	
	@RequestMapping(value="/login",method = RequestMethod.POST)
    @ResponseBody
    public User login(String username, String password, String role, HttpServletRequest request) {
        HttpSession session = request.getSession();
        
        System.out.println(session.getId());
        
        if(StringUtils.isEmpty(password)| StringUtils.isEmpty(role)){
        	 throw new RuntimeException("Password or role is empty");
        }
        
        User user = userService.getUserByUsername(username);
        if (user ==null){
        	throw new RuntimeException("Username is incorrect");
//        	return ReponseStatus.FAILED;
        }
        
        if (password.equals(user.getPassword()) && role.equals(user.getRole())){
        	session.setAttribute("user", user);
//        	return ReponseStatus.SUCCESS;
        	return user;
        }
        throw new RuntimeException("Login failed");
        
//        return ReponseStatus.FAILED;
    }
	

	@GetMapping("/isAuthenticated")
    @ResponseBody
	public boolean isAuthenticated(HttpServletRequest request){
		HttpSession session = request.getSession();
		
		System.out.println(session.getId());
		User user = (User)session.getAttribute("user");
		if(user!=null){
			return true;
		}else{
			return false;
		}	
	}
	
	@GetMapping("/getCurrentUser")
    @ResponseBody
	public User getCurrentUser(HttpServletRequest request){
		HttpSession session = request.getSession();
		
		User user = (User)session.getAttribute("user");
		return user;
	}
	
	@GetMapping("/logout")
    @ResponseBody
	public boolean logout(HttpServletRequest request){
		HttpSession session = request.getSession();
		
		session.removeAttribute("user");
		return true;
	}

	@RequestMapping(value="/reg",method = RequestMethod.POST)
	@ResponseBody
	public String reg(String username, String password, String role, HttpServletRequest request) {
		HttpSession session = request.getSession();

		System.out.println(session.getId());

		if(StringUtils.isEmpty(password)| StringUtils.isEmpty(role)){
			return ReponseStatus.FAILED;
		}
		User xuser;
		User user = userService.getUserByUsername(username);
		if (user !=null){
			return ReponseStatus.FAILED;
		}else{
			if(role.equals(User.STUDENT)){
				xuser = userService.createStudent(username,password);
			}else{
				xuser = userService.createTeacher(username,password);
			}
		}

		if (xuser!=null){
			return ReponseStatus.SUCCESS;
		}
		return ReponseStatus.FAILED;
	}
	

}
